package ccc.ClientSocketDemo3;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.google.gson.Gson;

import ccc.ClientSocketDemo3.R;

// 
public class ClientSocketDemo3 extends Activity {
	private static int serverPort = 9998;
	private static InetAddress serverIp ;
	
	public static Handler mHandler = new Handler();
	TextView TextView01;	// 用來顯示文字訊息
	EditText EditText01;	// 文字方塊
	EditText EditText02;	// 文字方塊
	String tmp;				// 暫存文字訊息
	Socket clientSocket;	// 客戶端socket
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		// 從資源檔裡取得位址後強制轉型成文字方塊
		TextView01 = (TextView) findViewById(R.id.TextView01);
		EditText01=(EditText) findViewById(R.id.EditText01);
		EditText02=(EditText) findViewById(R.id.EditText02);
		
		// 以新的執行緒來讀取資料
		Thread t = new Thread(readData);
		t.start();

		// 從資源檔裡取得位址後強制轉型成按鈕
		Button button1=(Button) findViewById(R.id.Button01);
		
		// 設定按鈕的事件
		//JSON版
		button1.setOnClickListener(new Button.OnClickListener() {		
			// 當按下按鈕的時候觸發以下的方法
			public void onClick(View v) {
				// 如果已連接則
				if(clientSocket.isConnected()){				
					BufferedWriter bw;
					
					try {
						// 取得網路輸出串流
						bw = new BufferedWriter( new OutputStreamWriter
								(clientSocket.getOutputStream(),"UTF-8"));
					
						bw.write(getGsonString()+"\n");
						//System.out.println(getGsonString());		
						// 立即發送
						bw.flush();
					} catch (IOException e) {					
					}							
					// 將文字方塊清空
					EditText02.setText("");
				}	
			}

		});
	

	}
	

	public String getGsonString() {
		SimpleDateFormat sdFormat = new SimpleDateFormat("a hh:mm:ss");
		Date today = new Date();
		Editable txtName = EditText01.getText();
		String name = txtName.toString();
		Editable txtMessage = EditText02.getText();
		String message = txtMessage.toString();		
		Message msg = new Message(name, message ,sdFormat.format(today));
		Gson gson = new Gson();			

		// 將Message物件轉成JSON
		String json = gson.toJson(msg);
		return json;
	}



	// 顯示更新訊息
	private Runnable updateText = new Runnable() {
		public void run() {
			// 加入新訊息並換行
			TextView01.append(tmp + "\n");
		}
	};

	// 取得網路訊息，這邊不需要改
	private Runnable readData = new Runnable() {
		public void run() {
			try {
				// 以內定(本機電腦端)IP為Server端
				serverIp = InetAddress.getByName("192.168.132.1");
				clientSocket = new Socket(serverIp, serverPort);			
				// 取得網路輸入串流
				BufferedReader br = new BufferedReader(new InputStreamReader(
						clientSocket.getInputStream()));
				
				// 當連線後
				while (clientSocket.isConnected()) {
					// 取得網路訊息
					tmp = br.readLine();					
					// 如果不是空訊息則
					if(tmp!=null)
						// 顯示新的訊息
						mHandler.post(updateText);
				}

			} catch (IOException e) {
				
			}
		}
	};
	
}